# -*-coding:utf-8 -*-
from xgoutils import XGOController
import socket

localhost = "0.0.0.0"
port = 8090
server_sk = socket.socket()
server_sk.bind((localhost, port))
server_sk.listen(5)
print("等待程序链接……")
conn, address = server_sk.accept()

# conn.sendall(bytes("连接成功！", encoding='utf-8'))
print('conn connection')
dog = XGOController("xgolite", conn)

while True:
    ret = str(conn.recv(1024).decode('utf-8'))
    if not ret:
        print("客户端关闭")
        break
    msglist = ret.split(",")
    print("收到客户端信息是：", msglist)
    if (len(msglist) > 2):
        if msglist[0] == 'q':
            break
        else:
            if msglist[2] == 'go':
                dog.go(msglist)
            if msglist[2] == 'tran':
                dog.tran(msglist)
            if msglist[2] == 'arm':
                dog.arm(msglist)
            # conn.sendall(bytes("执行完成", encoding='utf-8'))
    if (msglist[0] == 'p'):
        dog.setpace(msglist[1])
    if (msglist[0] == 'camera'):
        dog.cap = dog.setcamera()
        # print(dog.cap.isOpened())

    # if None is not dog.cap:
    #     if dog.cap.isOpened():
            # print('open')

    msglist.clear()
    print("内容清空：", len(msglist))
server_sk.close()
